// pagesEnterprise/pages/companyInsert/companyInsert.js
import ajax from "../../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailId: null, // 详情ID
    formData: {
      name: null, // 企业名称
      legalPerson: null, // 法人代表
      phone: null, // 企业联系电话，多个号码用英文逗号隔开

      operateName: null, // 运营人姓名
      operatePhone: null, // 运营人联系电话)
      creditCode: null, // 统一社会信用代码/纳税人识别号/组织结构代码
      roomNumber: null, // 
      province: null, // 省份 
      city: null, // 城市 
      county: null, // 区县 
      address: null, // 详细地址

      cardPhotoBack: null, // 身份证照片反面 
      cardPhotoJust: null, // 身份证照片正面
      indexImage: null, // 封面图片 
      licenseImage: null, // 营业执照图片
      adcode: null, // 区id
    }
  },
  /**
   * 房产地址 事件
   */
  addressEvent() {
    const { formData } = this.data
    wx.chooseLocation({
      complete: (location) => ajax.getQQMap().reverseGeocoder({
        location: { latitude: location.latitude, longitude: location.longitude },
        success: (res) => {
          const { ad_info , address_component, address_reference } = res.result
          formData.adcode = ad_info.adcode
          formData.province = address_component.province, formData.city = address_component.city, formData.county = address_component.district, formData.town = address_reference.town.title, formData.address = address_reference.town.title + address_component.street_number + address_reference.landmark_l2.title;
          this.setData({ formData })
          this.isSubmitChange()
        }
      })
    })
  },
  /**
   * input 变化事件
   */
  formChange(event) {
    const key = event.currentTarget.dataset.key
    const value = event.detail
    this.setData({ ['formData.' + key]: value })
    this.isSubmitChange()
  },
  /**
   * 校验字段
   */
  isSubmitChange() {
    const { roomNumber,name, legalPerson, phone, operateName, operatePhone, creditCode, province, city, county, address, cardPhotoBack, cardPhotoJust, indexImage, licenseImage } = this.data.formData;
    let boo = !(roomNumber && name && legalPerson && phone && operateName && operatePhone && creditCode && province && city && county && address && cardPhotoBack && cardPhotoJust && indexImage && licenseImage)
    this.setData({ isSubmit: !boo })
  },

  /**
   * 表单提交事件
   */
  async onSubmit() {
    ajax.showLoading()
    const { detailId } = this.data
    const formData = JSON.parse(JSON.stringify(this.data.formData))
    await ajax.post(detailId ? '/enterprise/update' : '/enterprise/manage/add', formData).finally(() => wx.hideLoading())
    await ajax.showToast('操作成功!')
    wx.navigateBack()
  },

  /**
   * 房产认证详情
   */
  async getDetailRequest() {
    const { detailId } = this.data
    const res = await ajax.get(`/enterprise/info?id=${detailId}`)
    this.setData({ formData: res.data })
    this.isSubmitChange()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({detailId: options.detailId || null}) 
    if (options.detailId) this.getDetailRequest()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})